<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button @click="addDialogVisible = true" type="primary" size="mini"
          >添加分类</el-button
        >
      </div>
      <el-table stype="width: 100%;" :data="cateList" border stripe>
        <el-table-column
          label="序号"
          type="index"
          width="100"
        ></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">

          <template v-slot="scope">
             <el-button @click="editOpenHandler(scope.row)" size="mini" type="primary" >修改</el-button>
          <el-button  @click="delHandler(scope.row.id)" type="danger" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加文章分类 -->
    <!-- :before-close="handleClose"> -->
    <!-- closed 关闭动画结束时的回调 -->
    <!-- resetFields 清空表单方法 -->
    <el-dialog
    @closed='$refs.addFrom.resetFields()'
      title="添加文章分类"
      :visible.sync="addDialogVisible"
      width="35%"
    >
      <el-form
        :model="addFrom"
        :rules="addRules"
        ref="addFrom"
        label-width="80px"
        class="demo-addFrom"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addFrom.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addFrom.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <!-- @click="addDdialogVisible = false" -->
      <span slot="footer" class="dialog-footer">
        <el-button  size="mini" @click="addDdialogVisible = false">取 消</el-button>
        <el-button  @click="addHandler" size="mini" type="primary"
          >添加</el-button
        >
      </span>
    </el-dialog>

    <!-- 修改分类对话框 -->
    <el-dialog
    @closed='$refs.editFrom.resetFields()'
      title="修改文章分类"
      :visible.sync="editDialogVisible"
      width="35%"
    >
      <el-form
        :model="editFrom"
        :rules="addRules"
        ref="editFrom"
        label-width="70px"
        class="demo-editFrom"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editFrom.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editFrom.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <!-- @click="addDdialogVisible = false" -->

      <span slot="footer" class="dialog-footer">
        <el-button  size="mini" @click="editDialogVisible = false">取 消</el-button>
        <el-button @click="editHandler"   size="mini" type="primary"
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  created () {
    this.getArtCate()
  },
  data () {
    return {
      cateList: [],
      addDialogVisible: false,
      addFrom: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '请输入1--10位的非空字符串', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类名必须是1-15位字母数字', trigger: 'blur' }
        ]
      },
      editDialogVisible: false,
      editFrom: {
        cate_name: '',
        cate_alias: ''
      }

    }
  },
  methods: {
    async getArtCate () {
      const { data: res } = await this.$http.get('/my/cate/list')
      if (res.code === 0) {
        this.cateList = res.data
      }
    },
    addHandler () {
      this.$refs.addFrom.validate(async result => {
        if (!result) return
        const { data: res } = await this.$http.post('/my/cate/add', this.addFrom)
        if (res.code === 0) {
          this.addDialogVisible = false
          this.getArtCate()
        }
      })
    },
    editHandler () {
      this.$refs.editFrom.validate(async result => {
        if (!result) return
        const { data: res } = await this.$http.put('/my/cate/info', this.editFrom)
        if (res.code === 0) {
          this.editDialogVisible = false
          this.getArtCate()
        }
      })
    },
    editOpenHandler (row) {
      if (row.id === 1 || row.id === 2) return this.$message.error('小B崽子,你想干哈')
      // 打开对话框
      this.editDialogVisible = true
      this.editFrom = { ...row }
    },
    async  delHandler (id) {
      if (id === 1 || id === 2) return this.$message.error('再动腿给你打断!')
      const result = await this.$confirm(
        '想清楚了没?', '小崽子.'
      ).catch((e) => e)
      if (result === 'confirm') {
        // 1. 发送请求删除数据
        // 有第二个参数的话,可以直接写数据对象表示 data请求体
        // 如果需要传入查询字符串,则需要手动指定 params
        const { data: res } = await this.$http.delete('/my/cate/del', {
          params: { id }
        })
        // 2. 更新数据
        if (res.code === 0) this.getArtCate()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-table {
  font-size: 12px;
}

// 全局重置 el-table 的样式
.el-table thead {
  color: #000;
}

.el-dialog__body {
  padding: 10px 20px;
}
</style>
